package com.fan.shop.views.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;

import com.fan.shop.businesslogic.TransactionReferenceCreateProcess;
import com.fan.shop.domain.TransactionReferenceGroup;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.services.TransactionReferenceGroupService;
import com.fan.shop.services.TransactionReferenceService;

@ManagedBean(name = "createTransactionReferenceBean")
@RequestScoped
public class CreateTransactionReferenceBean extends ShopBean {

	private static Logger logger = Logger
			.getLogger(CreateTransactionReferenceBean.class);

	@ManagedProperty(value = "#{transactionReferenceService}")
	private TransactionReferenceService transactionReferenceService;
	
	@ManagedProperty(value = "#{transactionReferenceGroupService}")
	private TransactionReferenceGroupService transactionReferenceGroupService;

	private String name;
	private String prefix;
	private int start = 1;
	private int last = 1;
	private int max = 99999;
	private String description;

	private String trGroupCode;
	private List<String> trGroupList;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		CreateTransactionReferenceBean.logger = logger;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		this.last = last;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getTrGroupCode() {
		return trGroupCode;
	}

	public void setTrGroupCode(String trGroupCode) {
		this.trGroupCode = trGroupCode;
	}

	public List<String> getTrGroupList() {
		trGroupList = new ArrayList<String>();
		for(TransactionReferenceGroup group : transactionReferenceGroupService.getAllTransactionReferenceGroups())
		{
			trGroupList.add(group.getName());
		}
		return trGroupList;
	}

	public void setTrGroupList(List<String> trGroupList) {
		this.trGroupList = trGroupList;
	}

	public TransactionReferenceGroupService getTransactionReferenceGroupService() {
		return transactionReferenceGroupService;
	}

	public void setTransactionReferenceGroupService(
			TransactionReferenceGroupService transactionReferenceGroupService) {
		this.transactionReferenceGroupService = transactionReferenceGroupService;
	}

	public String createNewTransactionReferenceAction() {
		TransactionReferenceCreateProcess transactionReferenceCreateProcess = new TransactionReferenceCreateProcess();
		transactionReferenceCreateProcess.setName(name);
		transactionReferenceCreateProcess.setGroupCode(trGroupCode);
		transactionReferenceCreateProcess.setDescription(description);
		transactionReferenceCreateProcess.setPrefix(prefix);
		transactionReferenceCreateProcess.setStart(start);
		transactionReferenceCreateProcess.setLast(last);
		transactionReferenceCreateProcess.setMax(max);
		transactionReferenceCreateProcess
				.setTransactionReferenceService(transactionReferenceService);

		int result = transactionReferenceCreateProcess.process();
		if (result == ShopGlobalVariables.SUCCESS) {
			return "management/transactionReferences";
		} else {
			return "management/newTransactionReference";
		}

	}

	public String backToTransactionReferencesAction() {
		return "management/transactionReferences";
	}

}
